#===istalismanplugin===
# -*- coding: utf-8 -*-

# $Revision: 54 $
# $Author: rchiganov@gmail.com $
# $Date: 2013-08-05 13:16:38 +0600 (Пн, 05 авг 2013) $

#---------------------------------------------------------------------------------------------------

#  Talisman plugin
#  delirium_plugin.py

#  Initial Copyright © 2007 Als <Als@exploit.in>
#  Modifications Copyright © 2010 Tuarisa <Tuarisa@gmail.com>

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

#---------------------------------------------------------------------------------------------------

def handler_afools_control(type, source, parameters):
	if parameters:
		try:
			int(parameters)
		except:
			reply(type,source,u'Команда написана неправильно')
		if int(parameters)>1:
			reply(type,source,u'Команда написана неправильно')
		if parameters=="1":
			GCHCFGS[source[1]]['afools']=1
			reply(type,source,u'шуточки включены')
		else:
			GCHCFGS[source[1]]['afools']=0
			reply(type,source,u'шуточки отключены')
	else:
		if GCHCFGS[source[1]]['afools']==1:
			reply(type,source,u'здесь шуточки включены')
		else:
			reply(type,source,u'здесь шуточки отключены')
	save_gch_cfg(source[1])

def get_afools_state(gch):
	if not 'afools' in GCHCFGS[gch]:
		GCHCFGS[gch]['afools']=1

def remix_string(parameters):
	remixed=[]
	for word in parameters.split():
		tmp=[]
		if len(word)<=1:
			remixed.append(word)
			continue
		elif len(word)==2:
			tmp=list(word)
			random.shuffle(tmp)
			remixed.append(u''.join(tmp))
		elif len(word)==3:
			tmp1=list(word[1:])
			tmp2=list(word[:-1])
			tmp=random.choice([tmp1,tmp2])
			if tmp==tmp1:
				random.shuffle(tmp)
				remixed.append(word[0]+u''.join(tmp))
			else:
				random.shuffle(tmp)
				remixed.append(u''.join(tmp)+word[-1])
		elif len(word)>=4:
			tmp=list(word[1:-1])
			random.shuffle(tmp)
			remixed.append(word[0]+u''.join(tmp)+word[-1])
	return u' '.join(remixed)

#---------------------------------------------------------------------------------------------------

if time.localtime()[1]==4 and time.localtime()[2]==1:
	def msg(target, body):
		if not isinstance(body, unicode):
			body = body.decode('utf8', 'replace')
		obody=body
		body=google_translate(body, u'ru', random.choice(trans_langs.keys()))
		msg = xmpp.Message(target)
		if GROUPCHATS.has_key(target):
			msg.setType('groupchat')
			if len(body)>1000:
				body=body[:1000]+u' >>>>'
			msg.setBody(body.strip())
		else:
			msg.setType('chat')
			msg.setBody(body.strip())
		JCON.send(msg)
		call_outgoing_message_handlers(target, body, obody)

register_command_handler(handler_afools_control, 'афолс', [ 'фан', 'мук', 'все' ], 30, 'Включает и выключает шуточки бота, которыми он порою подменяет (саму команду он всегда исполняет!) стандартный ответ команды.', 'афолс <1|0>', [ 'афолс 1', 'афолс 0' ])

register_stage1_init(get_afools_state)

#---------------------------------------------------------------------------------------------------